﻿/*------------------------------------------------------------------
-- COPYRIGHT (C) 2010-2012  Atom
-- ALL RIGHTS RESERVED.
-- 兆尹科技
-- CREATE DATE: 2010/07/12
-- CREATE MAN:liujian	
-- 日志管理类
-- MODIFY HISTORY:
-- MODIFY DATE:
-- MODIFY MAN:	
-- MODIFY DESC:
-- MODIFY DATE:
-- MODIFY MAN:	
-- MODIFY DESC:
---------------------------------------------------------------------*/
using System;
using System.Collections.Generic;
using System.Text;
using log4net;
using System.IO;

namespace Atom.Common
{
    /// <summary>
    /// log4Net的初始化
    /// </summary>
    public class Log4NetCommon
    {
        private static string configPath;
        private static string loggerName;
        private static ILog log = null;
        private static object lockHelper;

        static Log4NetCommon()
        {
        }

        /// <summary>
        /// 初始化配置文件名称
        /// </summary>
        /// <param name="logConfigPath">日志配置文件路径</param>
        /// <param name="loggerName">日志配置名称</param>
        public static void Setup(string logConfigPath, string loggerName)
        {
            log = null;
            lockHelper = new object();
            configPath = logConfigPath;
            Log4NetCommon.loggerName = loggerName;
        }

        /// <summary>
        /// 获取Log
        /// </summary>
        public static ILog Log
        {
            get
            {
                if (string.IsNullOrEmpty(configPath) || string.IsNullOrEmpty(loggerName))
                {
                    throw new NullReferenceException("在调用日志辅助类的时候，请先调用Setup初始化");
                }

                if (log == null || !log.Logger.Repository.Configured)
                {
                    lock (lockHelper)
                    {
                        if (log == null || !log.Logger.Repository.Configured)
                        {
                            log = LogManager.GetLogger(loggerName);
                            log4net.Config.XmlConfigurator.Configure(new FileInfo(configPath));                            
                        }
                    }
                }
                return log;
            }
        }   
    }
}
